using System;
using System.Collections.Generic;
using BikeSystem.Models;
using System.IO;
using Db4objects.Db4o;
using Db4objects.Db4o.Config;
using Db4objects.Db4o.Query;

namespace BikeSystem.DAL
{
    public class RentalService
    {
        readonly static string YapFileName = Path.Combine(Environment.CurrentDirectory, "bikesystem.yap");
        //
        public void Rental(Rental rental)
        {
            using (IObjectContainer db = Db4oEmbedded.OpenFile(YapFileName))
            {
                db.Store(rental);
                Console.WriteLine("Stored {0}", rental);
            }

        }

        public IList<Rental> GetAllRentals()
        {
            IList<Rental> rentals = new List<Rental>();

            using (IObjectContainer db = Db4oEmbedded.OpenFile(YapFileName))
            {
                IObjectSet result = db.QueryByExample(typeof(Rental));
                while (result.HasNext())
                {
                    rentals.Add((Rental)result.Next());
                }

                return rentals;
            };

        }


        public int UpdateRental(Rental rental)
        {

            IEmbeddedConfiguration config = Db4oEmbedded.NewConfiguration();
            config.Common.ObjectClass(typeof(Rental)).CascadeOnUpdate(true);
            using (IObjectContainer db = Db4oEmbedded.OpenFile(config, YapFileName))
            {
                Rental qbike = new Rental();
                qbike.RentNo = rental.RentNo;

                IObjectSet result = db.QueryByExample(qbike);
                if (result.HasNext())
                {

                    Rental uptbike = (Rental)result.Next();

                    uptbike.Bill = rental.Bill;
                    uptbike.IsReturn = rental.IsReturn;
                    uptbike.HasLock = rental.HasLock;
                    uptbike.UsedTime = rental.UsedTime;
                    db.Store(uptbike);
                    db.Commit();
                    return 0;
                }
                else
                {
                    return -1;
                }
            }

        }

        public Rental GetRental(string RentNo)
        {
            using (IObjectContainer db = Db4oEmbedded.OpenFile(YapFileName))
            {

                IQuery query = db.Query();
                query.Constrain(typeof(Rental));
                query.Descend("RentNo").Constrain(RentNo).Equal();
                IObjectSet result = query.Execute();
                if (result.HasNext())
                {
                    return (Rental)result.Next();
                }
                return null;
            };
        }

        public int DeleteRental(string rentNo)
        {
            IEmbeddedConfiguration config = Db4oEmbedded.NewConfiguration();
            config.Common.ObjectClass(typeof(Rental)).CascadeOnDelete(true);
            using (IObjectContainer db = Db4oEmbedded.OpenFile(config, YapFileName))
            {
                Rental rental = new Rental();
                rental.RentNo = rentNo;
                IObjectSet result = db.QueryByExample(rental);
                if (result.HasNext())
                {
                    db.Delete(result.Next());
                    return 0;
                }
                else
                {
                    return -1;
                }

            }
        }

    }
}
